package cn.tedu.egmvc3.controller;

import cn.tedu.egmvc3.mapper.UserMapper;
import cn.tedu.egmvc3.pojo.bto.UserInsertBTO;
import cn.tedu.egmvc3.pojo.entity.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/users/")
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @RequestMapping("insert")
    public String insert(UserInsertBTO userInsertBTO){
        User user = new User();
        BeanUtils.copyProperties(userInsertBTO,user);
        user.setCreated(new Date());
        int i = userMapper.insert(user);
        if (i>0){
            return "添加成功";
        }
        return "添加失败";
    }

    @RequestMapping("userList")
    public List<User> userList(){
        return userMapper.userList();
    }

}
